// mircoApp/ai-create/SpacePortal/index.js
let app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');

function debounce(func, wait) {
  let timeout;
  return function (...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => func.apply(this, args), wait);
  };
}
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    showTransfer: false,
    memberTransfer: {},
    interests_transfer: 0,
    list: [],
    member: {},
    member_statistics: {},
    percentage: 100,

    member_id: '',
    form: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: 'A' + 'I创作'
    });
    this.getData();
  },

  async getData() {
    let urlStr = app.getNetAddresss('plugin.ai-create.frontend.controllers.center.index');
    let response = await app._postNetWork({
      url: urlStr,
      data: {}
    });
    let {
      result,
      msg,
      data
    } = response.data;
    if (!result) return app.tips(msg);
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.themeBaseColor
    })
    let percentage = Number(data.video_duration.used) / Number(data.video_duration.total) * 100 || 0;
    data.percentage = percentage;
    this.setData(data);
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
    wx.navigateTo({
      url: url + "?" + _Param.slice(0, -1),
    });
  },
  onClickVal(e) {
    let {
      name,
      val
    } = e.currentTarget.dataset;
    this.setData({
      [name]: val
    });
  },
  transferIdinp: debounce(function (e) {
    if (this.data.member_id) this.getMember();
  }, 500),
  async getMember() {
    let urlStr = app.getNetAddresss('plugin.ai-create.frontend.controllers.member.transfer.get-member');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        member_id: this.data.member_id
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;
    if (!result) return app.tips(msg);
    this.setData({
      memberTransfer: data
    })
  },
  beforeBenefitList() {
    if (this.data.benefitList && this.data.benefitList.length) return;
    this.BenefitList();
  },
  async BenefitList() {
    if(this.Benefit_finished) return;
    let urlStr = app.getNetAddresss('plugin.ai-create.frontend.controllers.member.transfer.get-list');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        page: this.BenefitPage || 1
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;
    if (!result) return app.tips(msg);
    let benefitList = this.data.benefitList || [];
    this.setData({
      benefitList: benefitList.concat(data.data)
    });
    if(data.current_page == data.last_page) {
      return this.Benefit_finished = true;
    }
    this.BenefitPage = data.current_page + 1;
  },
  onClickRadio(event) {
    const {
      index
    } = event.currentTarget.dataset;
    const _data = this.data.benefitList[index];
    this.setData({
      activeBenefit: _data,
      transferID: _data.id
    });
  },
  onChangeField(event) {
    const {
      name, atter
    } = event.currentTarget.dataset;
    const val = event.detail;
    this.setData({
      ['form.' + name]: val >= this.data.activeBenefit[atter] ? this.data.activeBenefit[atter] : val
    })
  },
  async submitTransfer(){
    if(!this.data.memberTransfer.uid) {
      app.tips('请填写接收人ID');
      return;
    }
    if(!this.data.transferID) {
      app.tips('请选择有效权益');
      return;
    }
    let urlStr = app.getNetAddresss('plugin.ai-create.frontend.controllers.member.transfer.index');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        member_id: this.data.memberTransfer.uid,
        id: this.data.transferID,
        form: this.data.form,
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;
    if (!result) return app.tips(msg);
    this.setData({
      showTransfer: false,
      memberTransfer: null,
      activeBenefit: null
    })
  },
  onPopReachBottom(){
    this.BenefitList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      path: '/mircoApp/ai-create/SpacePortal/index',
    };
  }
})